package com.paic.custserv.web.controller.telAnswer;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.custserv.common.ResultMap;
import com.paic.custserv.common.enums.StatusEnum;
import com.paic.custserv.common.enums.WorkOrderStatus;
import com.paic.custserv.domain.AppUser;
import com.paic.custserv.domain.CussSystemConfig;
import com.paic.custserv.domain.CussWorkOrder;
import com.paic.custserv.domain.Page;
import com.paic.custserv.service.ICussWorkOrderService;
import com.paic.custserv.utils.AuthenticationUtils;
import com.paic.custserv.utils.CommonUtil;
import com.paic.custserv.web.controller.vo.CussWorkOrderQueryVO;

@RequestMapping("answer/workorder")
@Controller
public class AnswerWorkOrderController {
	private static final Logger logger = LoggerFactory.getLogger(AnswerWorkOrderController.class);

	@Autowired
	private ICussWorkOrderService cussWorkOrderService;
	
	/**
	 * 工单录入
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("entering.do")
	public String entering(Model model) {
    	try{     		
	    	String workOrderIncrCode=generateIncrCode().getValue();
	    	String currdate=CommonUtil.date2Str(new Date(), "yyyyMMdd");
			model.addAttribute("serialNumber", currdate+workOrderIncrCode);
  		}catch(Exception e) {
 			logger.error("客户专员查询待处理工单出错:{}", e);
 		}		
		return "tel_answer/entering";
	}
	
 	/**
 	 * 生成工单号中的自增部分code
 	 * 
 	 */
 	public synchronized CussSystemConfig generateIncrCode()  throws Exception{
 		CussSystemConfig  cussSysconf=cussWorkOrderService.selectByName("workOderIncrCode");
 		cussWorkOrderService.updateValueByName("workOderIncrCode",""+(Integer.valueOf(cussSysconf.getValue()).intValue()+1));
 		return cussSysconf;		
 	}
	
	/**
	 * 已录入的工单
	 * author zhouhuiqun470
	 * @param model
	 * @return
	 */
	@RequestMapping("entered.do")
	public String entered(Model model) {
		model.addAttribute("optype", "entered");
		return "tel_answer/entered";
	}
	
	/**
	 * 分页查询我录入的工单
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	@RequestMapping("entered/query.do")
	public @ResponseBody ResultMap enteredQuery(CussWorkOrderQueryVO vo) {
		try {
			return new ResultMap(StatusEnum.success, 
  					queryWorkOrder(vo));
		} catch (Exception e) {
			logger.error("电话接听员查询我录入的工单出错:{}", e);
		}
		return new ResultMap(StatusEnum.fail);
	}
	
	/**
	 * 录入工单
	 * 分配工单处理人(客服专员)，然后生成工单信息,并插入工单处理流水表(包括接听新建、未分配、分配待处理)
	 */	
	@RequestMapping(value ="/entering/add",method = {RequestMethod.POST})    
    public @ResponseBody String cussWorkOrderAdd(CussWorkOrder cussWorkOrder) {		
		try{
			String userAccount=AuthenticationUtils.getCurrentUser().getUserUMID();

			cussWorkOrder.setChannelSource("2");
			cussWorkOrder.setCreator(userAccount);			
			cussWorkOrder.setLastUpdatorUm(userAccount);
			
			//产生工单流水号
			cussWorkOrder.setSerialNumber(cussWorkOrder.getParentTypeCode()+"_"+cussWorkOrder.getTypeCode()+"_"+cussWorkOrder.getSerialNumber());//工单流水号，暂时只赋值工单类型号，在service中做处理
						
			cussWorkOrder.setWorkOrderStatus(WorkOrderStatus.Pending.getValue());//订单状态						
			
			cussWorkOrderService.insertCussWorkOrder(cussWorkOrder);

			return StatusEnum.success.getCode();
		}catch(Exception e){			
			logger.error("电话接听员插入工单信息时产生异常:{}", e);
		}		
		return StatusEnum.fail.getCode();
    }
	
	/**
	 * 查询工单信息
	 * @author 胡启萌
	 * @date 2015年9月15日
	 */
	private CussWorkOrderQueryVO queryWorkOrder(CussWorkOrderQueryVO vo){
		AppUser user = AuthenticationUtils.getCurrentUser();
		vo.setCreator(user.getUserUMID());
		List<CussWorkOrder> list = cussWorkOrderService.selectListByPage(vo);
		Page.setValues(vo, list);
		return vo;
	}
	
	/**
	 * 删除工单信息
	 */
	@RequestMapping(value ="/cussWorkOrderDelt.do",method = {RequestMethod.POST})    
    public @ResponseBody String cussWorkOrderDelt(HttpServletRequest request) {
		try{
			String workOrderId=request.getParameter("workOrderId");
			cussWorkOrderService.deleteCussWorkOrder(Integer.valueOf(workOrderId));
			return StatusEnum.success.getCode();
		}catch(Exception e){
			logger.error("删除工单信息时异常：", e);
			return StatusEnum.fail.getCode();
		}
	}					 		
	
}
